package cn.metafoo.security.core.aop;

import cn.metafoo.common.exception.ApiException;
import cn.metafoo.security.core.annotations.PreAuthenticated;
import cn.metafoo.security.core.utils.SecurityFrameworkUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import static cn.metafoo.common.exception.enums.GlobalErrorCodeConstants.UNAUTHORIZED;

/**
 * @author lordgod
 */
@Aspect
@Slf4j
public class PreAuthenticatedAspect {

    @Around("@annotation(preAuthenticated)")
    public Object around(ProceedingJoinPoint joinPoint, PreAuthenticated preAuthenticated) throws Throwable {
        if (SecurityFrameworkUtil.getLoginUser() == null) {
            throw new ApiException(UNAUTHORIZED);
        }
        return joinPoint.proceed();
    }

}
